package com.demo.amusic;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.demo.amusic.entity.Music;
import com.google.gson.Gson;

import java.util.List;
import java.util.Map;

public class PlayingListAdapter extends BaseAdapter {
    private Context context = null;
    // 数据
    private List<Music> list;
    // 界面
    private int resourceId;

    public PlayingListAdapter(Context context, List<Music> data, int resource) {
        this.context = context;
        list = data;
        resourceId = resource;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Music getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Music music = getItem(position);
        View view = null;
        // 新建视图
        view = View.inflate(context,R.layout.playing_list_item, null);

        Button btn = (Button) view.findViewById(R.id.playing_list_edit);
        TextView name = (TextView) view.findViewById(R.id.playing_list_name);
        TextView author = (TextView) view.findViewById(R.id.playing_list_author);
        TextView pos = (TextView) view.findViewById(R.id.playing_list_pos);
        name.setText(music.getName());
        author.setText(music.getAuthor());
        pos.setText(position + 1+"");
        btn.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,EditActivity.class);
                intent.putExtra("data",new Gson().toJson(music));
                context.startActivity(intent);
            }
        });
        return view;
    }

}
